//
//  ZKAddressChooseTableView.m
//  beibei_ios
//
//  Created by pro on 16/7/4.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ZKAddressChooseView.h"

#pragma mark - model
@implementation ZKAddressChooseModel

@end

@interface ZKAddressChooseCell : UITableViewCell
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, assign, getter=isChoosed) BOOL choosed;

@end


#pragma mark - cell
@implementation ZKAddressChooseCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        
        
        _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH, 35)];
        _label.font = FONT(14);
        _label.text = @"???";
        _label.textColor = [UIColor darkGrayColor];
        _label.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_label];
        
        _line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _label.width, 1)];
        _line.backgroundColor = HTB_THEME_COLOR;
        _line.hidden = YES;
        [_label addSubview:_line];
        _line.bottom = _label.height - 1;
    }
    return self;
}

- (void)setChoosed:(BOOL)choosed {
    _choosed = choosed;
    _label.textColor = _choosed ? HTB_THEME_COLOR : [UIColor darkGrayColor];
    _line.hidden = _choosed ? NO : YES;
}


@end


#pragma mark - main view

#define FIRST_BGCOLOR   [UIColor colorWithWhite:0.8 alpha:1]
#define SECOND_BGCOLOR  [UIColor colorWithWhite:0.88 alpha:1]
#define THIRD_BGCOLOR   [UIColor colorWithWhite:0.96 alpha:1]

@interface ZKAddressChooseView ()

/** view */
@property (nonatomic, strong) UITableView *firstTable;
@property (nonatomic, strong) UITableView *secondTable;
@property (nonatomic, strong) UITableView *thirdTable;
/** data */

@property (nonatomic, strong) NSArray *firstDatas;
@property (nonatomic, strong) NSArray *secondDatas;
@property (nonatomic, strong) NSArray *thirdDatas;
/** 记录 是否点击完成，用于刷新 */
@property (nonatomic, strong) NSMutableArray *firstSelectAry;
@property (nonatomic, strong) NSMutableArray *secondSelectAry;
@property (nonatomic, strong) NSMutableArray *thirdSelectAry;

/** 记录 点击的是哪一行 */
@property (nonatomic, assign) NSInteger firstSelect;
@property (nonatomic, assign) NSInteger secondSelect;


@end

@implementation ZKAddressChooseView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor redColor];
        
        NSMutableArray *allProvinceModelAry = [[NSMutableArray alloc] initWithArray:[ByCoreAddressSqlite3 getAllProvinceModel]];
        ByCoreProvinceModel *model = [[ByCoreProvinceModel alloc] init];
        model.province = @"全国";
        model.provinceID = @"-1";
        [allProvinceModelAry insertObject:model atIndex:0];
        self.firstDatas = [allProvinceModelAry copy];
        
        /** 选中行 初始化 */
        
        self.firstSelect = -1;
        self.secondSelect = -1;
        for (int i = 0; i < self.firstDatas.count; i ++) {
            [self.firstSelectAry addObject:@NO];
        }
    }
    return self;
}
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    self.firstTable.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    self.secondTable.frame = CGRectMake(frame.size.width / 3, 0, frame.size.width / 3 * 2, frame.size.height);
    self.secondTable.left = SCREEN_WIDTH;
    self.thirdTable.frame = CGRectMake(frame.size.width / 3 * 2, 0, frame.size.width / 3, frame.size.height);
    self.thirdTable.left = SCREEN_WIDTH;
}

- (UITableView *)firstTable {
    if (!_firstTable) {
        _firstTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _firstTable.backgroundColor = FIRST_BGCOLOR;
        _firstTable.delegate = self;
        _firstTable.dataSource = self;
        _firstTable.tag = 1;
        _firstTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_firstTable];
    }
    return _firstTable;
}
- (UITableView *)secondTable {
    if (!_secondTable) {
        _secondTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _secondTable.backgroundColor = SECOND_BGCOLOR;
        _secondTable.delegate = self;
        _secondTable.dataSource = self;
        _secondTable.tag = 2;
        _secondTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_secondTable];
    }
    return _secondTable;
}
- (UITableView *)thirdTable {
    if (!_thirdTable) {
        _thirdTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _thirdTable.backgroundColor = THIRD_BGCOLOR;
        _thirdTable.delegate = self;
        _thirdTable.dataSource = self;
        _thirdTable.tag = 3;
        _thirdTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_thirdTable];
    }
    return _thirdTable;
}
- (NSMutableArray *)firstSelectAry {
    if (!_firstSelectAry) {
        _firstSelectAry = [[NSMutableArray alloc] init];
    }
    return _firstSelectAry;
}
- (NSMutableArray *)secondSelectAry {
    if (!_secondSelectAry) {
        _secondSelectAry = [[NSMutableArray alloc] init];
    }
    return _secondSelectAry;
}
- (NSMutableArray *)thirdSelectAry {
    if (!_thirdSelectAry) {
        _thirdSelectAry = [[NSMutableArray alloc] init];
    }
    return _thirdSelectAry;
}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.firstTable) {
        return _firstDatas.count;
    } else if (tableView == self.secondTable) {
        return _secondDatas.count;
    } else {
        return _thirdDatas.count;
    }
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZKAddressChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZKAddressChooseCell"];
    if (cell == nil) {
        cell = [[ZKAddressChooseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ZKAddressChooseCell"];
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.contentView.frame];

    }

    if (tableView == self.firstTable) {
        cell.selectedBackgroundView.backgroundColor = SECOND_BGCOLOR;
        ByCoreProvinceModel *model = _firstDatas[indexPath.row];
        cell.choosed = [self.firstSelectAry[indexPath.row] boolValue];
        cell.label.text = model.province;
    } else if (tableView == self.secondTable) {
        cell.selectedBackgroundView.backgroundColor = THIRD_BGCOLOR;
        ByCoreCityModel *model = _secondDatas[indexPath.row];
        cell.choosed = [self.secondSelectAry[indexPath.row] boolValue];
        cell.label.text = model.city;
    } else {
        ByCoreAreaModel *model = _thirdDatas[indexPath.row];
        cell.choosed = [self.thirdSelectAry[indexPath.row] boolValue];
        cell.label.text = model.area;
    }
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 35;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}


// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    if (tableView == self.firstTable) {
        if (indexPath.row == _firstSelect && _secondSelect == -1) {
            return;
        }
        _firstSelect = indexPath.row;
        if (_secondSelect != -1) {
            self.secondTable.left = SCREEN_WIDTH;
            self.thirdTable.left = SCREEN_WIDTH;
            _secondSelect = -1;
        }
        /** 获得数据 */
        ByCoreProvinceModel *p_model = _firstDatas[indexPath.row];
        NSMutableArray *tmp = [[ByCoreAddressSqlite3 getAllCityModelWithProvinceID:p_model.provinceID] mutableCopy];
        if (tmp.count == 0) { /** 后面没有数据了，则搜索结束 */
            self.secondTable.left = SCREEN_WIDTH;
            self.thirdTable.left = SCREEN_WIDTH;
            
            for (int i = 0; i < self.firstSelectAry.count; i++) {
                if (i == indexPath.row) {
                    [self.firstSelectAry replaceObjectAtIndex:i withObject:@YES];
                } else {
                    [self.firstSelectAry replaceObjectAtIndex:i withObject:@NO];
                }
            }
            /** 代理回调 */
            if (self.delegate) {
                ZKAddressChooseModel *model = [self getAddressChooseModelWithProvinceModel:p_model cityModel:nil areaModel:nil];
                [self.delegate addressChooseViewDidChoose:model];
            }
            
            [tableView reloadData];
            return;
        }
        ByCoreCityModel *c_model = [[ByCoreCityModel alloc] init];
        c_model.city = [NSString stringWithFormat:@"全%@", p_model.province];
        c_model.cityID = @"-1";
        [tmp insertObject:c_model atIndex:0];
        _secondDatas = [tmp copy];
        /** 切换选择数组 */
        [self.secondSelectAry removeAllObjects];
        for (int i = 0; i < self.secondDatas.count; i ++) {
            [self.secondSelectAry addObject:@NO];
        }
        /** 动画 */
        [UIView animateWithDuration:0.15 animations:^{
            self.secondTable.right = SCREEN_WIDTH;
        }];
        [self.secondTable reloadData];
    }
    else if (tableView == self.secondTable) {
        if (indexPath.row == _secondSelect) {
            return;
        }
        _secondSelect = indexPath.row;
        /** 获得数据 */
        ByCoreCityModel *c_model = _secondDatas[indexPath.row];
        NSMutableArray *tmp = [[ByCoreAddressSqlite3 getAllAreaModelWithCityID:c_model.cityID] mutableCopy];
        if (tmp.count == 0) { /** 后面没有数据了，则搜索结束 */
            self.thirdTable.left = SCREEN_WIDTH;
            
            for (int i = 0; i < self.firstSelectAry.count; i++) {
                [self.firstSelectAry replaceObjectAtIndex:i withObject:@NO];
            }
            for (int i = 0; i < self.secondSelectAry.count; i++) {
                if (i == indexPath.row) {
                    [self.secondSelectAry replaceObjectAtIndex:i withObject:@YES];
                } else {
                    [self.secondSelectAry replaceObjectAtIndex:i withObject:@NO];
                }
            }
            /** 代理回调 */
            if (self.delegate) {
                ByCoreProvinceModel *p_model = _firstDatas[_firstSelect];
                ZKAddressChooseModel *model = [self getAddressChooseModelWithProvinceModel:p_model cityModel:c_model areaModel:nil];
                [self.delegate addressChooseViewDidChoose:model];
            }
            
            [_firstTable reloadData];
            [tableView reloadData];

            return;
        }
        ByCoreAreaModel *a_model = [[ByCoreAreaModel alloc] init];
        a_model.area = [NSString stringWithFormat:@"全%@", c_model.city];
        a_model.areaID = @"-1";
        [tmp insertObject:a_model atIndex:0];
        _thirdDatas = [tmp copy];
        /** 切换选择数组 */
        [self.thirdSelectAry removeAllObjects];
        for (int i = 0; i < self.thirdDatas.count; i ++) {
            [self.thirdSelectAry addObject:@NO];
        }
        /** 动画 */
        [UIView animateWithDuration:0.15 animations:^{
            self.thirdTable.right = SCREEN_WIDTH;
        }];
        [self.thirdTable reloadData];
    }
    else {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        for (int i = 0; i < self.firstSelectAry.count; i++) {
            [self.firstSelectAry replaceObjectAtIndex:i withObject:@NO];
        }
        for (int i = 0; i < self.secondSelectAry.count; i++) {
            [self.secondSelectAry replaceObjectAtIndex:i withObject:@NO];
        }
        for (int i = 0; i < self.thirdSelectAry.count; i++) {
            if (i == indexPath.row) {
                [self.thirdSelectAry replaceObjectAtIndex:i withObject:@YES];
            } else {
                [self.thirdSelectAry replaceObjectAtIndex:i withObject:@NO];
            }
        }
        /** 代理回调 */
        if (self.delegate) {
            ByCoreProvinceModel *p_model = _firstDatas[_firstSelect];
            ByCoreCityModel *c_model = _secondDatas[_secondSelect];
            ByCoreAreaModel *a_model = _thirdDatas[indexPath.row];
            ZKAddressChooseModel *model = [self getAddressChooseModelWithProvinceModel:p_model cityModel:c_model areaModel:a_model];
            [self.delegate addressChooseViewDidChoose:model];
        }
        
        [_firstTable reloadData];
        [_secondTable reloadData];
        [tableView reloadData];

        return;
    }
}



#pragma mark - 辅助
- (ZKAddressChooseModel *)getAddressChooseModelWithProvinceModel:(ByCoreProvinceModel *)pModel
                                     cityModel:(ByCoreCityModel *)cModel
                                     areaModel:(ByCoreAreaModel *)aModel {
    ZKAddressChooseModel *model = [[ZKAddressChooseModel alloc] init];
    model.province = pModel.province;
    model.provinceID = pModel.provinceID;
    model.city = cModel.city;
    model.cityID = cModel.cityID;
    model.area = aModel.area;
    model.areaID = aModel.areaID;
    
    return model;
}


@end
